from pymongo import MongoClient

# 创建MongoClient实例，默认连接到本地MongoDB服务
client = MongoClient('mongodb://localhost:27017/')

# 选择数据库，如果不存在则会自动创建
db = client['mydb1']

# 选择或创建集合
collection = db['mycollection']

# 插入单个文档
doc = {"_id": 1, "name": "李小红"}
collection.insert_one(doc)

# 插入多个文档
docs = [
    {"_id": 2, "name": "张三", "age": 20, "grade": "A"},
    {"_id": 3, "name": "李四", "age": 22, "grade": "B"}
]
collection.insert_many(docs)

# 关闭MongoClient连接
client.close()

print("数据插入完成")